package com.ruoyi.business.controller;

import java.util.List;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.UserCourse;
import com.ruoyi.business.service.IUserCourseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程用户映射Controller
 *
 * @author 吴倩芸
 * @date 2023-03-04
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/business/userCourse")
public class UserCourseController extends BaseController
{
    @Autowired
    private IUserCourseService userCourseService;

    /**
     * 查询课程用户映射列表
     */
    @PreAuthorize("@ss.hasPermi('business:user_course:query')")
    @GetMapping("/list")
    public TableDataInfo list(UserCourse userCourse)
    {
        startPage();
        List<UserCourse> list = userCourseService.selectUserCourseList(userCourse);
        return getDataTable(list);
    }

    @GetMapping("/isSubScribe")
    public AjaxResult isSubScribe(@RequestParam(value = "courseId", defaultValue = "") String courseId) {
        Boolean isSub = userCourseService.queryIsSubScribe(courseId, Long.toString(getUserId()));
        if (isSub) {
            return success(true);
        }else {
            return success(false);
        }
    }

    /**
     * 导出课程用户映射列表
     */
    @PreAuthorize("@ss.hasPermi('business:user_course:export')")
    @Log(title = "课程用户映射", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCourse userCourse)
    {
        List<UserCourse> list = userCourseService.selectUserCourseList(userCourse);
        ExcelUtil<UserCourse> util = new ExcelUtil<UserCourse>(UserCourse.class);
        util.exportExcel(response, list, "课程用户映射数据");
    }

    /**
     * 获取课程用户映射详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:user_course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCourseService.selectUserCourseById(id));
    }

    /**
     * 新增课程用户映射
     */
    @PreAuthorize("@ss.hasPermi('business:user_course:add')")
    @Log(title = "课程用户映射", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCourse userCourse)
    {
        return toAjax(userCourseService.insertUserCourse(userCourse));
    }

    /**
     * 修改课程用户映射
     */
    @PreAuthorize("@ss.hasPermi('business:user_course:edit')")
    @Log(title = "课程用户映射", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCourse userCourse)
    {
        return toAjax(userCourseService.updateUserCourse(userCourse));
    }

    /**
     * 删除课程用户映射
     */
    @PreAuthorize("@ss.hasPermi('business:user_course:remove')")
    @Log(title = "课程用户映射", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userCourseService.deleteUserCourseByIds(ids));
    }
}
